import { defineStore } from "pinia"
import type {
  category1Response,
  category2Response,
  category3Response,
} from "@/api/product/model/attrModel"
import {
  getgetcategory1Api,
  getgetcategory2Api,
  getgetcategory3Api,
} from "@/api/product/attr"
export interface Category {
  category1Id: number | undefined
  category2Id: number | undefined
  category3Id: number | undefined
  category1List: category1Response
  category2List: category2Response
  category3List: category3Response
}
export const useCategoryStore = defineStore("category", {
  state: (): Category => {
    return {
      category1Id: undefined, // 初始化为undefined，为了一上来select显示请选择xxx文字
      category2Id: undefined,
      category3Id: undefined,
      category1List: [],
      category2List: [],
      category3List: [],
    }
  },
  actions: {
    // 获取一级分类
    async getCategory1List() {
      const result = await getgetcategory1Api()
      this.category1List = result
    },
    // 当一级分类选择
    async getCategory2List(id: number) {
      const result = await getgetcategory2Api(id)
      this.category2List = result
      // 更新一级id
      this.category1Id = id
      // 清空三级分类数据&二级三级选中数据
      this.category3List = []
      this.category2Id = undefined
      this.category3Id = undefined
    },
    // 当二级分类选择
    async getCategory3List(id: number) {
      const result = await getgetcategory3Api(id)
      this.category3List = result
      // 更新一级id
      this.category2Id = id
      // 清空三级选中数据
      this.category3Id = undefined
    },
  },
})
